package com.tan.chowder.login.controller;

import com.tan.chowder.bean.Result;
import com.tan.chowder.login.entity.SysUserDO;
import com.tan.chowder.login.service.SysUserService;
import jakarta.annotation.Resource;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;


    @PostMapping("/login")
    @ResponseBody
    public Result login(String username, String password) {
        Result result = sysUserService.login(username, password);
        return result;
    }

    @RequestMapping("/test")
    public Result test() {
        return Result.success();
    }

    @RequestMapping("/save")
    public String save(SysUserDO sysUserDO) {
        sysUserService.save(sysUserDO);
        return "redirect:/user/findAll";
    }

    @RequestMapping("/findAll")
    public String findAll(Model model) {
        List<SysUserDO> sysUserDO = sysUserService.list();
        model.addAttribute("SysUserDOs", sysUserDO);
        return "sys-user/list";
    }
//
//    @RequestMapping("/getRolesByUser")
//    public String getRolesByUser(Model model, Integer uid) {
//        Map<String, Object> map = sysUserService.getRolesByUser(uid);
//        model.addAttribute("uid", uid);
//        model.addAttribute("allRoles", map.get("allRoles"));
//        model.addAttribute("hasRoles", map.get("hasRoles"));
//        return "/sys-user/roles";
//    }
//
//    @RequestMapping("/addRolesToUser")
//    public String getRolesByUser(Integer uid, Integer[] rids) {
//        sysUserService.addRolesToUser(uid, rids);
//        return "redirect:/user/getRolesByUser?uid=" + uid;
//    }

    @GetMapping("/logout")
    public Result logout(@RequestHeader("token") String token) {

        sysUserService.logout(token);

        return Result.success();
    }
}